/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package myauctioneer;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.stringtemplate.v4.ST;

public final class HelloServlet extends DefaultServlet
{
	public void indexAction(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();

		this.app.getLogger().info("Info log.");
		this.app.getLogger().warning("Warning log.");

		ST rootTpl = this.app.getTemplate("root");
//CHANGE TO DEDICATED FUNCTION ... START		
		/**
		 * Check if logged in as customer,
		 * if YES, don't allow to create new customer
		 */
		Customer customer = new Customer(this.app);
		customer.load(request.getSession());

		boolean isLoggedIn=false;
		try {
			isLoggedIn=customer.isLoggedIn(request.getSession());
		}
		catch (Exception e /*ServletException se*/) {
			rootTpl.add("msg", e.toString());
		}
		if (!isLoggedIn) rootTpl.add("login","");			
//CHANGE TO DEDICATED FUNCTION ... END		
		ST helloTpl = this.app.getTemplate("hello");

		rootTpl.add("title", "Application Test (HelloServlet)");

		helloTpl.add("request", request);
		rootTpl.add("content", helloTpl);

		writer.println(rootTpl.render());
	}

	public void databaseAction(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter writer = response.getWriter();

		ST rootTpl = this.app.getTemplate("root");
//CHANGE TO DEDICATED FUNCTION ... START		
		/**
		 * Check if logged in as customer,
		 * if YES, don't allow to create new customer
		 */
		Customer customer = new Customer(this.app);
		customer.load(request.getSession());

		boolean isLoggedIn=false;
		try {
			isLoggedIn=customer.isLoggedIn(request.getSession());
		}
		catch (Exception e /*ServletException se*/) {
			rootTpl.add("msg", e.toString());
		}
		if (!isLoggedIn) rootTpl.add("login","");			
//CHANGE TO DEDICATED FUNCTION ... END		
		ST helloTpl = this.app.getTemplate("database");

		try {
			rootTpl.add("title", "Database connection valid: " + String.valueOf(app.getDb().isValid(0)) + " (HelloServlet)");

			helloTpl.add("db", this.app.getDb());
			helloTpl.add("user", this.app.getProperty("db.user"));
			helloTpl.add("dbMeta", this.app.getDb().getMetaData());
		}
		catch(java.sql.SQLException e) {
			throw new ServletException(e.toString(), e);
		}

		rootTpl.add("content", helloTpl);

		writer.println(rootTpl.render());
	}
}
